const http = require('http')
const fs = require('fs')
const wait = seconds => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve()
        }, seconds * 1000)
    })
}
http.createServer((request, response) => {
    console.log('request come', request.url)
    if (request.url === '/') {
        const html = fs.readFileSync('text.html', 'utf-8')
        response.writeHead(200, {
            'Content-Type': 'text/html',
        })
        response.end(html)
    }

    if (request.url === '/data') {
        response.writeHead(200, {
            // 'Cache-Control': 'max-age=2, s-maxage=20'
            'Cache-Control': 's-maxage=200',
            'Vary':'X-Test-Cache'  // 值相同才会使用缓存
        })
        wait(2).then(() => {
            response.end('success')
        })
    }

}).listen(8888)
console.log('server listening on 8888')

